//
//  RecommendView.m
//  敢唆嘛!
//
//  Created by 教主 on 15/11/26.
//  Copyright © 2015年 zms. All rights reserved.
//

#import "RecommendView.h"
#import "Device.h"

@interface RecommendView () <UITextViewDelegate> //textView的代理
@property(nonatomic, strong)UILabel *placeHolderLabel;                      // 占位符
@property(nonatomic, strong)UIView *recommendBackgroundView;                // 创建主题灰色背景
@property(nonatomic, strong)UIImageView *userHeaderImage;                   // 用户头像
@property(nonatomic, strong)UILabel *userNameLabel;                         // 用户名
@property(nonatomic, strong)UIImageView *typeImage;                         // 类型图标
@property(nonatomic, strong)UILabel *typeLabel;                             // 类型
@property(nonatomic, strong)UILabel *infoTitleLabel;                        // 内容标题
@property(nonatomic, strong)UILabel *recommendNumberLabel;                  // 推荐数
@property(nonatomic, strong)UILabel *collectNumberLabel;                    // 收藏数
@property(nonatomic, strong)UILabel *commentNumberLabel;                    // 评论数
@property(nonatomic, strong)UILabel *createdAt;                             // 发布时间
@end

@implementation RecommendView
{
    CGFloat x;
    CGFloat y;
    CGFloat sx;
    CGFloat sy;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        x = KSWidth / 19;
        y = KSWidth / 19;
        sx = KSWidth / 25;
        sy = KSWidth / 25;
        // 添加写推荐语的textView
        self.recommendTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, KSWidth, KSHeight / 3.3)];
        self.recommendTextView.delegate = self;
        self.recommendTextView.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1.0];
        self.recommendTextView.font = [UIFont systemFontOfSize:KSWidth / 25];
        self.recommendTextView.textColor = [UIColor blackColor];
        [self addSubview:_recommendTextView];
        // 创建占位符
        self.placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, KSWidth - 5 * 2, KSWidth / 22)];
        self.placeHolderLabel.text = @"写段推荐语吧~";
        self.placeHolderLabel.textColor = [UIColor colorWithRed:160 / 255.0 green:160 / 255.0 blue:160 / 255.0 alpha:1.0];
        self.placeHolderLabel.font = [UIFont systemFontOfSize:KSWidth / 25];
        self.placeHolderLabel.enabled = NO;
        [self.recommendTextView addSubview:_placeHolderLabel];
        
        // 创建主题灰色背景
        CGFloat recommendTextViewHeight = self.recommendTextView.frame.size.height;
        self.recommendBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(x, recommendTextViewHeight + y, KSWidth - x * 2, KSWidth - x * 10)];
        self.recommendBackgroundView.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1.0];
        
        // 创建用户头像
        self.userHeaderImage = [[UIImageView alloc] initWithFrame:CGRectMake(sx, sy, KSWidth / 10, KSWidth / 10)];
        self.userHeaderImage.layer.cornerRadius = KSWidth / 20;
        self.userHeaderImage.layer.masksToBounds = YES; // 添加遮罩
        [self.recommendBackgroundView addSubview:_userHeaderImage];
        [self addSubview:_recommendBackgroundView];
        
        // 创建用户名
        CGFloat userHeaderImageWidth = self.userHeaderImage.frame.size.width;
        CGFloat recommendBackgroundViewWidth = self.recommendBackgroundView.frame.size.width;
        self.userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(userHeaderImageWidth + sx * 2, sy, recommendBackgroundViewWidth - userHeaderImageWidth - sx * 7, KSWidth / 19)];
        self.userNameLabel.alpha = 0.8;
        self.userNameLabel.textColor = [UIColor blackColor];
        self.userNameLabel.font = [UIFont systemFontOfSize:KSWidth / 28];
        self.userNameLabel.textAlignment = NSTextAlignmentLeft;
        [self.recommendBackgroundView addSubview:_userNameLabel];
        
        // 创建类型图标
        CGFloat userNameLabelWidth = self.userNameLabel.frame.size.width;
        self.typeImage = [[UIImageView alloc] initWithFrame:CGRectMake(userHeaderImageWidth + userNameLabelWidth + sx * 3, sy, KSWidth / 26, KSWidth / 26)];
        [self.recommendBackgroundView addSubview:_typeImage];
        
        // 创建类型
        CGFloat typeImageWidth = self.typeImage.frame.size.width;
        self.typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(userHeaderImageWidth + userNameLabelWidth + typeImageWidth + sx * 3.5, sy, KSWidth - userHeaderImageWidth - userNameLabelWidth - typeImageWidth - sx * 7, KSWidth / 26)];
        self.typeLabel.textColor = [UIColor blackColor];
        self.typeLabel.alpha = 0.6;
        self.typeLabel.font = [UIFont systemFontOfSize:KSWidth / 34];
        [self.recommendBackgroundView addSubview:_typeLabel];
        
        // 创建内容标题
        CGFloat userHeaderImageHeight = self.userHeaderImage.frame.size.height;
        self.infoTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(sx, userHeaderImageHeight + sy * 2, recommendBackgroundViewWidth - sy * 2, KSWidth / 20)];
        self.infoTitleLabel.alpha = 0.9;
        self.infoTitleLabel.textColor = [UIColor blackColor];
        self.infoTitleLabel.font = [UIFont systemFontOfSize:KSWidth / 20];
        [self.recommendBackgroundView addSubview:_infoTitleLabel];
        
        // 创建装载三个评论数的view
        CGFloat infoTitleLabelHeight = self.infoTitleLabel.frame.size.height;
        UIView *downView = [[UIView alloc] initWithFrame:CGRectMake(KSWidth / 2.3, userHeaderImageHeight + infoTitleLabelHeight + sy * 6.5, KSWidth - (KSWidth / 2.3) - sy * 4, KSWidth / 29)];
        //        self.downView.backgroundColor = [UIColor redColor];
        [self.recommendBackgroundView addSubview:downView];
        
        // 创建推荐数图标
        UIImageView *recommendNumberImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KSWidth / 27, KSWidth / 27)];
        recommendNumberImage.image = [UIImage imageNamed:@"iconfont-xunhuan2"];
        [downView addSubview:recommendNumberImage];
        
        // 创建推荐数
        CGFloat recommendNumberImageWidth = recommendNumberImage.frame.size.width;
        self.recommendNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + 5, 0, KSWidth / 14, KSWidth / 27)];
        self.recommendNumberLabel.alpha = 0.4;
        self.recommendNumberLabel.textColor = [UIColor blackColor];
        self.recommendNumberLabel.font = [UIFont systemFontOfSize:KSWidth / 31];
        [downView addSubview:_recommendNumberLabel];
        
        // 创建收藏图标
        CGFloat recommendNumberLabelWidth = self.recommendNumberLabel.frame.size.width;
        UIImageView *collectNumberImage = [[UIImageView alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + recommendNumberLabelWidth + sx, 0, KSWidth / 27, KSWidth / 27)];
        collectNumberImage.image = [UIImage imageNamed:@"iconfont-xingxing"];
        [downView addSubview:collectNumberImage];
        
        // 创建收藏数
        CGFloat collectNumberImageWidth = collectNumberImage.frame.size.width;
        self.collectNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + recommendNumberLabelWidth + collectNumberImageWidth + sx + 5, 0, KSWidth / 14, KSWidth / 27)];
        self.collectNumberLabel.alpha = 0.4;
        self.collectNumberLabel.textColor = [UIColor blackColor];
        self.collectNumberLabel.font = [UIFont systemFontOfSize:KSWidth / 31];
        [downView addSubview:_collectNumberLabel];
        
        // 创建评论图标
        CGFloat collectNumberLabelWidth = self.collectNumberLabel.frame.size.width;
        UIImageView *commentNumberImage = [[UIImageView alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + recommendNumberLabelWidth + collectNumberImageWidth + collectNumberLabelWidth + sx * 2, 0, KSWidth / 27, KSWidth / 27)];
        commentNumberImage.image = [UIImage imageNamed:@"iconfont-duihuaqipao"];
        [downView addSubview:commentNumberImage];
        
        // 创建评论数
        CGFloat commentNumberImageWidth = commentNumberImage.frame.size.width;
        self.commentNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(recommendNumberImageWidth + recommendNumberLabelWidth + collectNumberImageWidth + collectNumberLabelWidth + commentNumberImageWidth + sx * 2 + 5, 0, KSWidth / 14, KSWidth / 27)];
        self.commentNumberLabel.alpha = 0.4;
        self.commentNumberLabel.textColor = [UIColor blackColor];
        self.commentNumberLabel.font = [UIFont systemFontOfSize:KSWidth / 31];
        [downView addSubview:_commentNumberLabel];
        
    }
    return self;
}

- (void)setHomeModel:(HomeModel *)model
{
    [self.userHeaderImage sd_setImageWithURL:[NSURL URLWithString:model.userHeader]];
    self.userNameLabel.text = model.userName;
    self.typeLabel.text = model.type;
    
    // 判断类型 来给图片
    if ([self.typeLabel.text isEqualToString:@"真事"])
    {
        self.typeImage.image = [UIImage imageNamed:@"iconfont-yuanquan"];
    }
    else if ([self.typeLabel.text isEqualToString:@"创作"])
    {
        self.typeImage.image = [UIImage imageNamed:@"iconfont-yuanquan-3"];
    }
    else if ([self.typeLabel.text isEqualToString:@"游记"])
    {
        self.typeImage.image = [UIImage imageNamed:@"iconfont-yuanquan-2"];
    }
    else if ([self.typeLabel.text isEqualToString:@"秘密"])
    {
        self.typeImage.image = [UIImage imageNamed:@"iconfont-yuanquan-1"];
    }
    self.infoTitleLabel.text = model.infoTitle;
    
    self.recommendNumberLabel.text = [NSString stringWithFormat:@"%ld",model.recommendNumber];
    self.collectNumberLabel.text = [NSString stringWithFormat:@"%ld",model.collectNumber];
    self.commentNumberLabel.text = [NSString stringWithFormat:@"%ld",model.commentNumber];
}

- (void)setRealityModel:(RealityModel *)model
{
    [self.userHeaderImage sd_setImageWithURL:[NSURL URLWithString:model.userHeader]];
    self.userNameLabel.text = model.userName;
    self.typeLabel.text = model.type;
    
    // 判断类型 来给图片
    if ([self.typeLabel.text isEqualToString:@"真事"])
    {
        self.typeImage.image = [UIImage imageNamed:@"iconfont-yuanquan"];
    }
    else if ([self.typeLabel.text isEqualToString:@"创作"])
    {
        self.typeImage.image = [UIImage imageNamed:@"iconfont-yuanquan-3"];
    }
    else if ([self.typeLabel.text isEqualToString:@"游记"])
    {
        self.typeImage.image = [UIImage imageNamed:@"iconfont-yuanquan-2"];
    }
    else if ([self.typeLabel.text isEqualToString:@"秘密"])
    {
        self.typeImage.image = [UIImage imageNamed:@"iconfont-yuanquan-1"];
    }
    self.infoTitleLabel.text = model.infoTitle;
    
    self.recommendNumberLabel.text = [NSString stringWithFormat:@"%ld",model.recommendNumber];
    self.collectNumberLabel.text = [NSString stringWithFormat:@"%ld",model.collectNumber];
    self.commentNumberLabel.text = [NSString stringWithFormat:@"%ld",model.commentNumber];
}

// UITextView代理方法  当开始编辑的时候 占位字符取消
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    _placeHolderLabel.text = @"";
    return YES;
}

#pragma mark ----- 点击空白回收键盘 -----
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{    //如果回收键盘后 没有写内容 依然使用占位字符
    if (_recommendTextView.text.length == 0)
    {
        _placeHolderLabel.text = @"写段推荐语吧~";
    }
    [_recommendTextView resignFirstResponder];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
